# 作业一：
# 在服务器上自己的crmeb
# 1、登录
# 2、添加用户分组——》删除刚才新增的用户分组
# 3、添加是否成功，断言；删除是否成功，断言
# 组件化，元素与脚本分离
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from ele import *
# deiver = webdriver.Chrome()
# deiver.get("http://120.26.126.215:8899/admin/login?redirect=%2Fadmin%2Findex")
# time.sleep(2)
# deiver.maximize_window()
# deiver.find_elements(By.CLASS_NAME,"el-input__inner")[0].send_keys("admin")
# deiver.find_elements(By.CLASS_NAME,"el-input__inner")[1].send_keys("123456")
# deiver.find_element(By.CLASS_NAME,"el-button").click()
# time.sleep(2)
# deiver.find_elements(By.CLASS_NAME,"font12")[1].click()
# time.sleep(2)
# deiver.find_element(By.XPATH,"//*[text()='用户分组']").click()
# time.sleep(2)
# deiver.find_element(By.XPATH,"//*[text()='添加分组']").click()
# time.sleep(2)
# deiver.find_elements(By.CLASS_NAME,"el-input__inner")[2].send_keys('张鑫')
# time.sleep(2)
# deiver.find_elements(By.CLASS_NAME,"el-button")[2].click()
# time.sleep(2)
# #断言
# text = deiver.find_element(By.XPATH,"//*[@id='app']/section/div[2]/section/div/div[1]/div/main/div/div[1]/div/div[1]/div/div/div/div[2]/div[3]/table/tbody/tr[5]/td[2]/div/span").text
# assert text == "张鑫"
# time.sleep(3)
# deiver.find_elements(By.XPATH,"//*[text()='删除']")[-1].click()
# time.sleep(2)
# deiver.find_elements(By.CLASS_NAME,"el-button")[2].click()
# time.sleep(2)

#组件化

class Crmeb:
    def __init__(self,url,driver):
        self.driver = driver
        self.driver.get(url)
        self.driver.maximize_window()
        self.driver.implicitly_wait(3)
    def login(self,username,password):
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(user1))[0].send_keys(username)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(pwd1))[1].send_keys(password)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(dengl)).click()
        time.sleep(2)
    def add_fenzu(self,neirong):
        self.neirong = neirong
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(yongh))[1].click()
        WebDriverWait(self.driver,10,0.5).until((EC.presence_of_element_located(fenzu))).click()
        WebDriverWait(self.driver,10,0.5).until((EC.presence_of_element_located(tianjia))).click()
        time.sleep(2)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(shuru))[2].send_keys(self.neirong)
        time.sleep(2)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(queren))[2].click()
        time.sleep(2)
    def duanyan(self):
        text = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,f"//*[text()='{self.neirong}']"))).text
        assert text == self.neirong
        time.sleep(2)
    def delete_1(self):
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(shanchu))[-1].click()
        time.sleep(2)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(queren2))[2].click()
        time.sleep(2)
if __name__ == '__main__':
    driver = webdriver.Chrome()
    url = "http://120.26.126.215:8899/admin/login?redirect=%2Fadmin%2Findex"
    c1 = Crmeb(url, driver)
    c1.login("admin", "123456")
    c1.add_fenzu("张鑫")
    c1.duanyan()
    c1.delete_1()


# 作业二：
# QQ音乐登录：https://y.qq.com/portal/pop_login.html
"""
deiver = webdriver.Chrome()
deiver.get("https://y.qq.com/portal/pop_login.html")
time.sleep(2)
deiver.maximize_window()
time.sleep(2)
iframe = deiver.find_elements(By.TAG_NAME,"iframe")
deiver.switch_to.frame(iframe[0])
deiver.find_element(By.ID,"img_out_2205187147").click()
time.sleep(2)
deiver.find_elements(By.CLASS_NAME,"inputstyle")[0].send_keys('2205187147')
time.sleep(2)
deiver.find_elements(By.CLASS_NAME,"inputstyle")[1].send_keys('18888888')   #密码
time.sleep(2)
deiver.find_element(By.CLASS_NAME,"btn").click()
"""

# 作业三-crmeb：
# 1、添加用户标签——》删除刚才新增的用户标签
# 2、添加是否成功，断言；删除是否成功，断言
# deiver = webdriver.Chrome()
# deiver.get("http://120.26.126.215:8899/admin/login?redirect=%2Fadmin%2Findex")
# deiver.maximize_window()
# deiver.find_elements(By.CLASS_NAME,"el-input__inner")[0].send_keys("admin")
# deiver.find_elements(By.CLASS_NAME,"el-input__inner")[1].send_keys("123456")
# deiver.find_element(By.CLASS_NAME,"el-button").click()
# time.sleep(2)
# deiver.find_elements(By.CLASS_NAME,"font12")[1].click()
# time.sleep(2)
# deiver.find_element(By.XPATH,"//*[text()='用户标签']").click()
# time.sleep(2)
# deiver.find_element(By.XPATH,"//*[text()='添加标签']").click()
# time.sleep(2)
# #下拉
# deiver.find_elements(By.CLASS_NAME,"el-select__caret")[1].click()
# time.sleep(2)
# #选择
# deiver.find_elements(By.CLASS_NAME,"el-select-dropdown__item")[6].click()
# deiver.find_elements(By.CLASS_NAME,"el-input__inner")[3].send_keys('张鑫')
# time.sleep(2)
#
# deiver.find_elements(By.CLASS_NAME,"el-button")[2].click()
# deiver.find_elements(By.CLASS_NAME,"number")[1].click()
# time.sleep(2)
# text = deiver.find_element(By.XPATH,"//*[@id='app']/section/div[2]/section/div/div[1]/div/main/div/div[1]/div/div[1]/div/div/div[2]/div/div/div[2]/div[3]/table/tbody/tr[13]/td[2]/div/span").text
# assert text == "张鑫"
# time.sleep(2)
# deiver.find_elements(By.XPATH,"//*[text()='删除']")[-1].click()
# time.sleep(2)
# deiver.find_elements(By.CLASS_NAME,"el-button")[2].click()

class Crmeb:
    def __init__(self,url,driver):
        self.driver = driver
        self.driver.get(url)
        self.driver.maximize_window()
        self.driver.implicitly_wait(3)
    def login(self,username,password):
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(user1))[0].send_keys(username)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(pwd1))[1].send_keys(password)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(dengl)).click()
        time.sleep(2)
    def add_fenzu(self,neirong):
        self.neirong = neirong
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(yongh))[1].click()
        WebDriverWait(self.driver,10,0.5).until((EC.presence_of_element_located(biaoqian))).click()

        WebDriverWait(self.driver,10,0.5).until((EC.presence_of_element_located(tianjia1))).click()
        time.sleep(2)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(xiala))[1].click()
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(xuanze))[6].click()
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(shuru1))[3].send_keys(self.neirong)
        time.sleep(2)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(queren3))[2].click()
        time.sleep(2)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(fy))[1].click()
    def duanyan(self):
        text = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(dy)).text

        time.sleep(2)
        print(f"text 的值: {text}")  # 打印 text 的值
        print(f"self.neirong 的值: {self.neirong}")  # 打印 self.neirong 的值
        assert text == self.neirong  # 执行断言，查看是否相等
    def delete_1(self):
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(shanchu1))[-1].click()
        time.sleep(2)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(queren4))[2].click()
        time.sleep(2)
if __name__ == '__main__':
    driver = webdriver.Chrome()
    url = "http://120.26.126.215:8899/admin/login?redirect=%2Fadmin%2Findex"
    c1 = Crmeb(url, driver)
    c1.login("admin", "123456")
    c1.add_fenzu("张鑫")
    c1.duanyan()
    c1.delete_1()
